using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MissionTargetPickUpBonuse : MissionTargetBase
{
	[Serializable]
	protected class BonusSpawn : IComparable
	{
		public idBonuse bonuseID;

		public Vector3 bonusePosition;

		[HideInInspector]
		public BonuseItem bonuseItem;

		[HideInInspector]
		public PointerPanel pointer;

		[HideInInspector]
		public float playerDistance;

		[HideInInspector]
		public bool collected;

		public bool needPointer = true;

		public int CompareTo(object other)
		{
			return playerDistance.CompareTo((other as BonusSpawn).playerDistance);
		}
	}

	[SerializeField]
	protected List<BonusSpawn> bonusSpawn;

	[SerializeField]
	protected bool useOneIndicator;

	protected PointerPanel pointer;

	private Coroutine showNearestItem;

	protected override bool OnStartTarget()
	{
		if (!base.OnStartTarget())
		{
			return false;
		}
		foreach (BonusSpawn item in bonusSpawn)
		{
			item.bonuseItem = bonuseManager.thisScript.InstantBonuse(item.bonuseID, false, item.bonusePosition, 0, 0f).GetComponent<BonuseItem>();
			item.bonuseItem.destroyWhenCollection = true;
			item.bonuseItem.collectAnyway = true;
			item.bonuseItem.UseTimerToDestroy = false;
			if (!useOneIndicator && item.needPointer)
			{
				item.pointer = PointerController.instance.AddPointer(item.bonuseItem.transform, 0f, "iconMap_Red Cercle");
			}
			item.collected = false;
		}
		curCountTarget = 0;
		if (useOneIndicator)
		{
			showNearestItem = StartCoroutine(ShowNearestItem());
		}
		return true;
	}

	public override void OnUpdateTarget()
	{
		for (int i = 0; i < bonusSpawn.Count; i++)
		{
			if (!bonusSpawn[i].collected && bonusSpawn[i].bonuseItem == null)
			{
				if ((bool)bonusSpawn[i].pointer)
				{
					PointerController.instance.RemovePointer(bonusSpawn[i].pointer);
				}
				bonusSpawn[i].collected = true;
				curCountTarget++;
			}
		}
		if (bonusSpawn.TrueForAll((BonusSpawn b) => b.bonuseItem == null))
		{
			CompleteTarget();
		}
		curParametr.UpdateValue();
	}

	private IEnumerator ShowNearestItem()
	{
		while (bonusSpawn.Count > 0)
		{
			Transform playerTransform = GameController.instance.playerScript.transform;
			bonusSpawn.ForEach(delegate(BonusSpawn b)
			{
				b.playerDistance = ((!(b.bonuseItem != null)) ? float.PositiveInfinity : Vector3.Distance(playerTransform.position, b.bonuseItem.transform.position));
			});
			BonusSpawn min = bonusSpawn.Min();
			if (min.bonuseItem != null)
			{
				if (pointer == null)
				{
					pointer = PointerController.instance.AddPointer(min.bonuseItem.transform, 0f, "iconMap_Red Cercle");
				}
				else
				{
					pointer.target = min.bonuseItem.transform;
				}
			}
			else if (pointer != null)
			{
				pointer.target = null;
			}
			yield return new WaitForSeconds(0.5f);
		}
	}

	public override void OnStopTarget()
	{
		base.OnStopTarget();
		foreach (BonusSpawn item in bonusSpawn)
		{
			if ((bool)item.pointer)
			{
				PointerController.instance.RemovePointer(item.pointer);
			}
			if ((bool)item.bonuseItem)
			{
				item.bonuseItem.DisableBonuse();
			}
		}
		if (showNearestItem != null)
		{
			StopCoroutine(showNearestItem);
		}
		if ((bool)pointer)
		{
			PointerController.instance.RemovePointer(pointer);
		}
	}

	protected override bool TargetIsComplete()
	{
		return false;
	}
}
